package abel.wlmr.model.type;

import abel.wlmr.model.WlmrField;

/**
 * 脉冲计数
 * 异常（0-
 * 正常， 1-
 * 异常）
 * 直读异常
 * （0 正常/1
 * 异常）
 * 备用
 * 流量传感器状态
 * （00 正常/01 超上限）
 * 10 超下限 11 断线）
 * 压力传感器状态
 * （00 正常/01 超上限）
 * 10 超下限 11 断线）
 * 
 * @author Abel
 *
 */
public class MeterStatus1Field extends WlmrField {

    private SensorStatusType pressStatus = SensorStatusType.Ready;

    private SensorStatusType flowStatus = SensorStatusType.Ready;

    private byte readingException;

    private byte pulseException;

    public MeterStatus1Field() {
        super(2);
    }

    @Override
    public int getValue() {
        if (pressStatus == null) {
            throw new IllegalArgumentException("press status should not be null");
        }
        if (flowStatus == null) {
            throw new IllegalArgumentException("flow status should not be null");
        }
        if (readingException != 0 && readingException != 1) {
            throw new IllegalArgumentException("reading exception should be [0,1]");
        }
        if (pulseException != 0 && pulseException != 1) {
            throw new IllegalArgumentException("pulse exception should be [0,1]");
        }

        return pulseException << 7 | readingException << 6 | flowStatus.getValue() << 2 | pressStatus.getValue();
    }

    @Override
    public void parseValue(int v) {
        pressStatus = SensorStatusType.findEnumByValue((byte) (0x03 & v));

        flowStatus = SensorStatusType.findEnumByValue((byte) ((0x0C & v) >> 2));

        readingException = (byte) ((0x40 & v) >> 6);

        pulseException = (byte) ((0x80 & v) >> 7);
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return String.format("{pressStatus:%s, flowStatus:%s, readingException:%s, pulseException:%s}", pressStatus,
            flowStatus, readingException, pulseException);
    }

    /**
     * @return the pressStatus
     */
    public SensorStatusType getPressStatus() {
        return pressStatus;
    }

    /**
     * @param pressStatus
     *            the pressStatus to set
     */
    public void setPressStatus(SensorStatusType pressStatus) {
        this.pressStatus = pressStatus;
    }

    /**
     * @return the flowStatus
     */
    public SensorStatusType getFlowStatus() {
        return flowStatus;
    }

    /**
     * @param flowStatus
     *            the flowStatus to set
     */
    public void setFlowStatus(SensorStatusType flowStatus) {
        this.flowStatus = flowStatus;
    }

    /**
     * @return the readingException
     */
    public byte getReadingException() {
        return readingException;
    }

    /**
     * @param readingException
     *            the readingException to set
     */
    public void setReadingException(byte readingException) {
        this.readingException = readingException;
    }

    /**
     * @return the pulseException
     */
    public byte getPulseException() {
        return pulseException;
    }

    /**
     * @param pulseException
     *            the pulseException to set
     */
    public void setPulseException(byte pulseException) {
        this.pulseException = pulseException;
    }

}
